<?php
/**
 * Created by PhpStorm.
 * User: zcs
 * Date: 17/1/9
 * Time: 上午11:07
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Need_ajax extends CRUD_Controller {
    public function __construct()
    {
        parent::__construct();
        header("Content-type: application/json");
        date_default_timezone_set('Asia/Shanghai');
        $this->load->model('account_model','maccount');
        $this->load->model('needs_model','mneeds');
        $this->load->model('claim_model','mclaim');
        $this->load->model('pay_logs_model', 'mpay_log');
        $this->load->model('countdown_model','mcount_down');
        $this->load->model('balance_detail_model','mbalance');
        $this->load->model('need_order_model','mneedorder');
        $this->load->model('need_apply_model','mneedapply');
    }

    /**
     * 发布方仲裁处理
     * 处理修改议价价格
     */
    public function arbitrate()
    {
        $params = $this->input->post();
        $need_id = $params['need_id'];
        if(!$need_id){
            echo json_encode(array('status'=>false,'msg'=>'请选择相应的需求'));exit;
        }
        $update_money = $params['change_money'];
        if(!$update_money){
            echo json_encode(array('status'=>false,'msg'=>'请填写修改的价格'));exit;
        }
        //处理相应的业务逻辑
        $claim_info = $this->mclaim->get_one(array('need_id'=>$need_id,'is_assign'=>1));
        if(!empty($claim_info)){
            $this->mclaim->update(array('money'=>$update_money,'update_time'=>time()),array('id'=>$claim_info['id']));
            $this->mneeds->update(array('update_money'=>$update_money,'arbitrate'=>2,'update_time'=>time()),array('id'=>$need_id));
            echo json_encode(array('status'=>true,'msg'=>'处理成功'));exit;
        }else{
            echo json_encode(array('status'=>false,'msg'=>'操作失败'));exit;
        }
    }

    /**
     * 认领方仲裁处理
     * 确认并评价
     */
    public function c_arbitrate()
    {
        $params = $this->input->post();
        $need_id = $params['need_id'];
        if(!$need_id){
            echo json_encode(array('status'=>false,'msg'=>'请选择相应的需求'));exit;
        }
        $stars = $params['stars'];
        if(!$stars){
            echo json_encode(array('status'=>false,'msg'=>'请选择评星'));exit;
        }

        $claim_info = $this->mclaim->get_one(array('need_id'=>$need_id,'is_assign'=>1));
        if(empty($claim_info)){
            echo json_encode(array('status'=>false,'msg'=>'操作失败'));exit;
        }else{
            //查询需求的详细信息
            $need_info = $this->mneeds->get_one(array('id'=>$claim_info['need_id']));
            if($need_info['claim_status']==4){
                //确认评价处理(已完成)
                $this->mclaim->update(array('stars'=>$stars,'arbitrate'=>2),array('id'=>$claim_info['id']));
                $this->mneeds->update(array('claim_status'=>5,'updated'=>time(),'c_arbitrate'=>2,'finished'=>time()),array('id'=>$need_info['id']));
                //更新账户余额处理和评星处理(待完善)
                //认领方余额增加议价价格,明细记录修改状态为1成功
                //获取该认领人已获得的评价总星数
                $user_id = $need_info['uid'];
                $claim_user_id = $claim_info['uid'];
                $claims = $this->db->query("select count(*) as counts,sum(stars) as total from boc_claim where uid=$claim_user_id and is_assign=1")->result_array();
                if(!empty($claims)){
                    //$now_stars  评星
                    $total = $claims[0]['total'];
                    $counts = $claims[0]['counts'];
                    $r_stars = round($total/$counts);
                    $correct = $total/$counts;
                    if($correct == $r_stars){
                        $now_stars = $correct;
                    }elseif($correct > $r_stars-0.5){
                        $now_stars = $r_stars;
                    }elseif($correct < $r_stars + 0.5){
                        $now_stars = $r_stars;
                    }elseif($correct == $r_stars-0.5){
                        $now_stars = $correct;
                    }
                }else{
                    $now_stars = $stars;
                }
                //增加认领数和评星数以及余额更新+明细记录  悬赏收入
                //悬赏收入
                $money_new = $claim_info['money'];
                $this->db->query("update boc_account set balance=balance+$money_new,reward=reward+$money_new,stars=$now_stars,claims=claims+1 where id=$claim_user_id");
                $this->mbalance->update(array('status'=>1,'rid'=>$claim_user_id,'updated_at'=>time()),array('need_id'=>$need_info['id']));   //成功
                $balance_data_in = array(
                    'uid'=>$claim_user_id,
                    'type'=>5,              //悬赏收入
                    'title'=>$need_info['intro'],
                    'need_id'=>$need_info['id'],
                    'money'=>$money_new,
                    'status'=>1,
                    'pay_ways'=>3,          //余额收入
                    'pay_time'=>time(),
                    'created_at'=>time(),
                    'updated_at'=>time(),
                    'rid'=>$user_id,
                    'payment_sn'=>$need_info['payment_sn'],
                    'pay_code'=>$need_info['order_sn'],
                );
                //处理悬赏差价退回
                //查询补差价订单金额
                $need_order_info = $this->mneedorder->get_one(array('need_id'=>$need_id));
                $need_order_money = 0;
                if(!empty($need_order_info)){
                    $need_order_money = $need_order_info['money'];
                }
                $all_money = $need_order_money + $need_info['margin'];
                if($all_money > $money_new){
                    $surpose_money = $all_money - $money_new;
                    $this->db->query("update boc_account set balance=balance+$surpose_money where id=$user_id");
                    $balance_data_in2 = array(
                        'uid'=>$user_id,
                        'type'=>7,              //悬赏差价退回
                        'title'=>$need_info['intro'],
                        'need_id'=>$need_info['id'],
                        'money'=>$surpose_money,
                        'status'=>1,
                        'pay_ways'=>3,          //余额收入
                        'pay_time'=>time(),
                        'created_at'=>time(),
                        'updated_at'=>time(),
                        'rid'=>0,
                        'payment_sn'=>$need_info['payment_sn'],
                        'pay_code'=>$need_info['order_sn'],
                    );
                    $this->mbalance->create($balance_data_in2);
                }
                $this->mbalance->create($balance_data_in);
                /*******添加推送板块**********/
                $to_uid = $claim_info['uid'];
                $t_user_info = $this->maccount->get_one(array('id'=>$need_info['uid']));
                $uname = $t_user_info['uname'];
                $t_type = 1;
                $content = $uname.'已支付悬赏佣金';
                getui_push($to_uid,$t_type,$claim_info['need_id'],$content);
                echo json_encode(array('status'=>true,'msg'=>'处理成功'));exit;
            }else{
                echo json_encode(array('status'=>false,'msg'=>'操作失败'));exit;
            }
        }
    }
}
